<template>
  <!-- 新增部门的弹层 -->
  <el-dialog
    :title="form.id ? '编辑部门' : '新增部门'"
    :visible="visible"
    v-bind="$attrs"
    v-on="$listeners"
    @open="onOpen"
    @close="onClose"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      size="medium"
      label-width="100px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          placeholder="1-50个字符"
          clearable
          :style="{ width: '100%' }"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          placeholder="1-50个字符"
          clearable
          :style="{ width: '100%' }"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          placeholder="请选择部门负责人"
          clearable
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="(item, index) in managerOptions"
            :key="index"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          type="textarea"
          placeholder="1-300个字符"
          :autosize="{ minRows: 4, maxRows: 4 }"
          :style="{ width: '100%' }"
        ></el-input>
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-button
        type="primary"
        size="small"
        @click="handelConfirm"
      >确定</el-button>
      <el-button size="small" @click="close">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeesSimple } from '@/api/employees.js'
import { addDept, getList, updateDept } from '@/api/departments.js'
export default {
  name: '',
  components: {},
  inheritAttrs: false,
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    node: {
      type: Object,
      default: () => ({}),
      require: true
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      let brothers
      if (this.form.id) {
        // 编辑
        const allDepts = (await getList()).depts
        brothers = allDepts.filter(t => t.id !== this.form.id && t.id !== this.form.id)
      } else {
        // 新增
        brothers = this.node.children || []
      }

      const f = brothers.find(t => t.name === value)
      if (!f) {
        callback()
      } else {
        callback(new Error('存在重复兄弟部门编码'))
      }
    }
    const checkCode = async(rule, value, callback) => {
      const allDepts = (await getList()).depts
      let f
      if (this.form.id) {
        f = allDepts.filter(t => t.id !== this.form.id).find(t => t.code === value)
      } else {
        // 新增
        f = allDepts.find(t => t.code === value)
      }
      if (!f) {
        callback()
      } else {
        callback(new Error('存在重复编号的部门'))
      }
    }
    return {
      form: {
        name: undefined,
        code: undefined,
        manager: undefined,
        introduce: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入单行文本部门名称',
          trigger: 'blur'
        },
        { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'blur' },
        { validator: checkName, trigger: 'blur' }],
        code: [{
          required: true,
          message: '请输入部门编码',
          trigger: 'blur'
        }, { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'blur' },
        { validator: checkCode, trigger: 'blur' }],
        manager: [{
          required: true,
          message: '请输入部门负责人',
          trigger: 'change'
        }],
        introduce: [{
          required: true,
          message: '请输入部门介绍',
          trigger: 'blur'
        }, { min: 1, max: 300, message: '请输入1-300个字符', trigger: 'blur' }]
      },
      managerOptions: []

    }
  },
  async created() {
    this.managerOptions = await getEmployeesSimple()
  },
  mounted() { },
  methods: {
    onOpen() { },
    onClose() {
      // 对整个表单进行重置,将所有字段重置为初始值,并移除校验结果
      this.$refs['formRef'].resetFields()
      this.form = {
        name: undefined,
        code: undefined,
        manager: undefined,
        introduce: undefined
      }
    },
    close() {
      this.$emit('update:visible', false)
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        // 发请求
        if (this.form.id) {
          // 编辑
          await updateDept(this.form)
        } else {
          // 新增
          await addDept({
            ...this.form,
            pid: this.node.pid
          })
        }

        this.$message.success('操作成功')
        this.$emit('success')
        this.close()
      })
    }

  }
}
</script>

<style lang="scss" scoped>
</style>
